import { DataTypes, Model } from 'sequelize'
import sequelize from './db'

export interface IUser {
    id: number
    userId: string
    nickname: string
    avatar: string
    name: string
    xueyuan: string
    banji: string
}

class User extends Model {
    declare id: number
    declare userId: string
    declare nickname: string
    declare avatar: string
    declare name: string
    declare xueyuan: string
    declare banji: string
    declare role: 'admin' | 'teacher' | 'student' | 'zhujiao'
}

User.init(
    {
        id: {
            type: DataTypes.INTEGER,
            primaryKey: true,
            autoIncrement: true,
        },
        userId: {
            type: DataTypes.STRING,
            allowNull: false,
            comment: '用户id，学号或工号',
        },
        nickname: {
            type: DataTypes.STRING,
            allowNull: false,
            comment: '用户昵称',
            defaultValue: '新用户'
        },
        password: {
            type: DataTypes.STRING,
            allowNull: false,
            comment: '用户密码',
            defaultValue: '123456',
            get() {
                return '******'
            }
        },
        avatar: {
            type: DataTypes.STRING,
            comment: '用户头像',
        },
        name: {
            type: DataTypes.STRING,
            comment: '用户真实姓名',
        },
        email: {
            type: DataTypes.STRING,
            comment: '用户邮箱',
        },
        xueyuan: {
            type: DataTypes.STRING,
            comment: '学院',
        },
        zhuanye: {
            type: DataTypes.STRING,
            comment: '专业',
        },
        banji: {
            type: DataTypes.STRING,
            comment: '班级',
        },
        role: {
            type: DataTypes.STRING,
            comment: '角色, admin, teacher, zhujiao, student',
            defaultValue: 'student',
        },
    },
    {
        // 这是其他模型参数
        sequelize, // 我们需要传递连接实例
        modelName: 'user', // 我们需要选择模型名称
        paranoid: true,
    }
)

export default User
